package com.jinboe.order.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jinboe.comm.service.impl.BaseServiceImpl;
import com.jinboe.comm.util.BeanUtils;
import com.jinboe.comm.vo.CommPage;
import com.jinboe.order.dao.OrderDao;
import com.jinboe.order.dto.OrderItemDto;
import com.jinboe.order.dto.OrderMainDto;
import com.jinboe.order.service.OrderService;
import com.jinboe.order.util.OrderConstants;
import com.jinboe.order.vo.OrderItemVo;
import com.jinboe.order.vo.OrderMainVo;

public class OrderServiceImpl extends BaseServiceImpl implements OrderService{

	private OrderDao orderDao ;
	
	@Override
	public String createOrderItem(OrderItemVo orderItemVo) {
		// TODO Auto-generated method stub
		OrderItemDto orderItemDto=new OrderItemDto();
		BeanUtils.copyProperties(orderItemDto, orderItemVo);
		return orderDao.insertOrderItem(orderItemDto);
	}

	@Override
	public String createOrderMain(OrderMainVo orderMainVo, String[] deskIds) {
		// TODO Auto-generated method stub
		OrderMainDto orderMainDto=new OrderMainDto();
		BeanUtils.copyProperties(orderMainDto, orderMainVo);
		orderMainDto.setStats(OrderConstants.VALUE_ORDERSTATS_DEFAULT);
		String orderMainId=orderDao.insertOrderMain(orderMainDto);
		if(deskIds!=null&&deskIds.length>0){
			//insert deskMapping 
		}
		return orderMainId;
	}

	@Override
	public OrderMainVo getOrderMainInfo(String mainId) {
		// TODO Auto-generated method stub
		OrderMainDto orderMainDto=orderDao.queryOrderMain(mainId);
		OrderMainVo orderMainVo=new OrderMainVo();
		BeanUtils.copyProperties(orderMainVo, orderMainDto);
		List<OrderItemDto> itemList=orderDao.queryOrderItem(orderMainDto.getId());
		if(itemList!=null){
			List <OrderItemVo> voList=new ArrayList();
			for(OrderItemDto orderItemDto:itemList){
				OrderItemVo orderItemVo=new OrderItemVo();
				BeanUtils.copyProperties(orderItemVo, orderItemDto);
				voList.add(orderItemVo);
			}
			orderMainVo.setItemList(voList);
		}
		{
			//还应该有去处地应台位信息
		}
		return orderMainVo;
	}

	@Override
	public int operateOrderMain(String mainId, Float payCost,
			Float discount, Integer stats) {
		// TODO Auto-generated method stub
		OrderMainDto orderMainDto=orderDao.queryOrderMain(mainId);
		orderMainDto.setPayCost(payCost);
		orderMainDto.setDiscount(discount);
		if (stats!=null) {
			orderMainDto.setStats(stats);
		}
		return orderDao.updateOrderMain(orderMainDto);
	}

	@Override
	public CommPage queryOrderMainInfo(CommPage commPage, Map conMap) {
		// TODO Auto-generated method stub
		int noteCount=orderDao.queryOrderMainCount(conMap);
		commPage.setNoteCount(noteCount);
		List <OrderMainDto> mainDtoList=orderDao.queryOrderMain(conMap, commPage.getNoteStart(), commPage.getNoteEnd());
		if(mainDtoList==null){
			return commPage;
		}
		List <OrderMainVo> mainVoList=new ArrayList();
		for(OrderMainDto orderMainDto:mainDtoList){
			OrderMainVo orderMainVo =new OrderMainVo();
			BeanUtils.copyProperties(orderMainVo, orderMainDto);
			mainVoList.add(orderMainVo);
		}
		commPage.setCommList(mainVoList);
		return commPage;
	}

	//---------------------------------
	public OrderDao getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}
	
}
